package ui2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import node.Node;
import node.NodeDescriptor;

public class UIMainController implements ActionListener{
	// The Model
	private Node theNode;
	
	// The View
	private Menu theView;
	
	public UIMainController(String configFilePath, boolean discoveryMode)
	{
		super();

		// Start the Model
		theNode = new Node(configFilePath, discoveryMode);
		theView = null;
	}
	
	public void start()
	{
		// Start the View
		final UIMainController controller = this;
		EventQueue.invokeLater(new Runnable() {
            public void run() {
                    try {
                            theView = new Menu();
                            for(int i=0; i<theView.getMenuBar().getMenuCount(); i++)
                            {
                            	JMenu currentMenu = theView.getMenuBar().getMenu(i);
                            	for(int j=0; j<currentMenu.getItemCount(); j++)
                            	{
                            		currentMenu.getItem(j).addActionListener(controller);
                            	}
                            }
                            
                            theView.getFrame().setVisible(true);
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }
		});
	}
	
	public static void main(String[] args) {
		if(args.length > 0 && args.length < 3)
		{
			boolean discoveryMode = false;
			if(args.length==2)
			{
				int discoveryModeInt = Integer.parseInt(args[1]);
				discoveryMode = discoveryModeInt > 0;
			}
			UIMainController theController = new UIMainController(args[0], discoveryMode);
			theController.start();
		}
		else
		{
			System.out.println("iDistribute - Error: Config File Path Missing. Please retry.");
			System.exit(0);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() instanceof JMenuItem)
		{
			JMenuItem menuItem = (JMenuItem) arg0.getSource();
			menuHandler(menuItem.getText());
		}
	}
	
	private void menuHandler(String itemTitle)
	{	
		JPanel tempPanel = null;
		
		if(itemTitle.equalsIgnoreCase("PC")==true)
		{
			tempPanel = new PanelPc(	NodeDescriptor.getInstance().getNcpu(), 
										NodeDescriptor.getInstance().getFreq(),
										NodeDescriptor.getInstance().getMem());	
		}
		
		if(itemTitle.equalsIgnoreCase("Port / IP") == true)
		{
			tempPanel = new PanelPort(	NodeDescriptor.getInstance().getIp(),
										NodeDescriptor.getInstance().getETS().getPort(),
										NodeDescriptor.getInstance().getEES().getPort(),
										NodeDescriptor.getInstance().getFsPort());
		}
		
		if(itemTitle.equalsIgnoreCase("Queue") == true)
		{
			tempPanel = new PanelQueue( NodeDescriptor.getInstance().getISubmission(),
										NodeDescriptor.getInstance().getISize(),
										NodeDescriptor.getInstance().getESize(),
										NodeDescriptor.getInstance().getIMaxThreads(),
										NodeDescriptor.getInstance().getEMaxThreads(),
										NodeDescriptor.getInstance().getI_timeout(),
										NodeDescriptor.getInstance().getE_timeout());
		}
		
		if(itemTitle.equalsIgnoreCase("Discovery") == true)
		{
			//tempPanel = new PanelDiscovery();
		}
		
		theView.setMainPanel(tempPanel);
	}
}
